class WordExtractorService {
  /**
   * Extract words from text
   * @param {string} text - Text to extract words from
   * @returns {Array} - Array of extracted words
   */
  extractWords(text) {
    if (!text || typeof text !== 'string') {
      return [];
    }

    // Remove extra whitespace and trim
    const cleanText = text.trim();

    if (cleanText === '') {
      return [];
    }

    // Split by whitespace and common punctuation, then filter
    const words = cleanText
      .split(/[\s\-\.\,\;\:\!\?\(\)\[\]\{\}\"\'\/\\]+/)
      .map(word => word.trim())
      .filter(word => word.length > 0 && /^[a-zA-Z]+$/.test(word)) // Only alphabetic words
      .map(word => word.toLowerCase()); // Convert to lowercase

    // Remove duplicates
    return [...new Set(words)];
  }

  /**
   * Extract word at specific position in text
   * @param {string} text - Text to extract word from
   * @param {number} position - Position in text
   * @returns {string|null} - Extracted word or null if not found
   */
  extractWordAtPosition(text, position) {
    if (!text || typeof text !== 'string' || position < 0 || position >= text.length) {
      return null;
    }

    // Find word boundaries
    let start = position;
    let end = position;

    // Move start to the beginning of the word
    while (start > 0 && /[a-zA-Z]/.test(text[start - 1])) {
      start--;
    }

    // Move end to the end of the word
    while (end < text.length && /[a-zA-Z]/.test(text[end])) {
      end++;
    }

    // Extract the word
    const word = text.substring(start, end);

    // Validate that it's a proper word
    if (word.length > 0 && /^[a-zA-Z]+$/.test(word)) {
      return word.toLowerCase();
    }

    return null;
  }

  /**
   * Filter out common English words from a list
   * @param {string[]} words - Array of words to filter
   * @returns {string[]} - Filtered array of words
   */
  filterCommonWords(words) {
    // Common English words to filter out (simplified list)
    const commonWords = new Set(this.constructor.commonWordsList);
    
    return words.filter(word => !commonWords.has(word.toLowerCase()));
  }
}

// Common words list shared with migrateVocabularyData.js
const commonWordsList = [
  'the', 'be', 'to', 'of', 'and', 'a', 'in', 'that', 'have', 'i',
  'it', 'for', 'not', 'on', 'with', 'he', 'as', 'you', 'do', 'at',
  'this', 'but', 'his', 'by', 'from', 'they', 'she', 'or', 'an', 'will',
  'my', 'one', 'all', 'would', 'there', 'their', 'what', 'so', 'up', 'out',
  'if', 'about', 'who', 'get', 'which', 'go', 'me', 'when', 'make', 'can',
  'like', 'time', 'no', 'just', 'him', 'know', 'take', 'people', 'into', 'year',
  'your', 'good', 'some', 'could', 'them', 'see', 'other', 'than', 'then', 'now',
  'look', 'only', 'come', 'its', 'over', 'think', 'also', 'back', 'after', 'use',
  'two', 'how', 'our', 'work', 'first', 'well', 'way', 'even', 'new', 'want',
  'because', 'any', 'these', 'give', 'day', 'most', 'us', 'is', 'was', 'are',
];

module.exports = WordExtractorService;
module.exports.commonWordsList = commonWordsList;
